/*
 * CanvasControl.cpp
 *
 *  Created on: 2013-1-26
 *      Author: heqiu
 */

#include "Global.h"

void button_building_icon_pressed(void * pData, GuudButton * pButton) {
	CanvasControl * pCanvasControl = (CanvasControl*) pData;
	pCanvasControl->m_bListen_icon = !pCanvasControl->m_bListen_icon;
	pCanvasControl->m_select_building = pButton->m_sName;
	pCanvasControl->m_p_select_building =
			pCanvasControl->m_map_button_building[pCanvasControl->m_select_building.c_str()];
}

void button_building_pressed(void * pData, GuudButton * pButton) {
	CanvasControl * pCanvasControl = (CanvasControl*) pData;
	if (pCanvasControl->m_p_select_building != NULL) {
		pCanvasControl->m_select_building.clear();
		pCanvasControl->m_p_select_building = NULL;
		return;
	}
	pCanvasControl->m_bListen_icon = !pCanvasControl->m_bListen_icon;
}
CanvasControl::CanvasControl(uint32_t uiShortUnit, uint32_t uiLongUnit,
		uint32_t uiUnitPixel) :
		GuudCanvas(uiShortUnit, uiLongUnit, uiUnitPixel) {
	m_p_select_building = NULL;
	m_bListen_icon = false;
}
CanvasControl::~CanvasControl() {
	map<string, GuudBuilding *>::iterator it;
	for (it = m_map_button_building.begin(); it != m_map_button_building.end();
			it++) {
		delete it->second;
	}
	m_map_button_building.clear();
}
void CanvasControl::draw() {
	if (m_bShow) {

		GuudPoint stPoint = m_gpScreenPosPixel;
		if (m_p_select_building != NULL) {
			stPoint.m_iX = -200 + g_GlobalData.m_GameListener.m_iMouseX;
			stPoint.m_iY = -150 + g_GlobalData.m_GameListener.m_iMouseY;
			g_GlobalData.m_pGuudScreen->drawButton(stPoint,
					*m_p_select_building);
		}
		stPoint = m_gpScreenPosPixel;
		g_GlobalData.m_pGuudScreen->drawButton(stPoint, m_button_building);
		if (m_bListen_icon) {
			GuudRect rect;
			rect.x = 150;
			rect.y = 20;
			rect.w = 500;
			rect.h = 200;
			GuudColor color;
			color.setColor(183, 183, 183);
			g_GlobalData.m_pGuudScreen->drawRect(rect, color);
			stPoint.m_iX += 200;
			map<string, GuudButton *>::iterator it;
			int iNum = 0;
			for (it = m_map_button_building_icon.begin();
					it != m_map_button_building_icon.end(); it++) {
				g_GlobalData.m_pGuudScreen->drawButton(stPoint, *it->second);
				stPoint.m_iX += 100;
				iNum++;
				if (iNum % 4 == 0) {
					stPoint.m_iY += 100;
					stPoint.m_iX -= 400;
				}
			}
		}

	}
}
void CanvasControl::initBuilding(GuudRect & rect, const char * pName) {
	GuudBuilding * pGuudButton = NULL;
	pGuudButton = new GuudBuilding();
	pGuudButton->m_iXLenUnit = 2;
	pGuudButton->m_iYLenUnit = 2;
	pGuudButton->setSurface(&m_GuudSurface_building, rect, rect, rect);
	pGuudButton->m_sName = pName;
	m_map_button_building[pGuudButton->m_sName] = pGuudButton;
}
void CanvasControl::initBuildingIcon(GuudRect & rect, const char * pName) {
	GuudButton * pGuudButton = NULL;
	pGuudButton = new GuudButton();
	pGuudButton->setSurface(&m_GuudSurface_building_icon, rect, rect, rect);
	pGuudButton->m_sName = pName;
	pGuudButton->setFuncPressed(button_building_icon_pressed);
	m_map_button_building_icon[pGuudButton->m_sName] = pGuudButton;
}
void CanvasControl::init() {
	{
		string sFileName = "./image/control.png";
		bool bFlag = m_GuudSurface_control.loadImage(sFileName.c_str());
		if (!bFlag) {
			g_GlobalData.m_GuudLog.log("[%s][%d]错误:图片 %s 加载失败", __FILE__,
					__LINE__, sFileName.c_str());
			exit(-1);
		}
		GuudRect rectBuilding;
		rectBuilding.w = 100;
		rectBuilding.h = 50;
		rectBuilding.x = 0;
		rectBuilding.y = 0;
		m_button_building.setSurface(&m_GuudSurface_control, rectBuilding,
				rectBuilding, rectBuilding);
		m_button_building.setFuncPressed(button_building_pressed);
	}
	{
		string sFileName = "./image/building.png";
		bool bFlag = m_GuudSurface_building.loadImage(sFileName.c_str());
		if (!bFlag) {
			g_GlobalData.m_GuudLog.log("[%s][%d]错误:图片 %s 加载失败", __FILE__,
					__LINE__, sFileName.c_str());
			exit(-1);
		}
		GuudRect rectBuilding;
		rectBuilding.w = 200;
		rectBuilding.h = 200;
		//木头
		rectBuilding.x = 0;
		rectBuilding.y = 0;
		initBuilding(rectBuilding, "mutou");
		//石头
		rectBuilding.x = 200;
		rectBuilding.y = 0;
		initBuilding(rectBuilding, "shitou");
		//粮食
		rectBuilding.x = 400;
		rectBuilding.y = 0;
		initBuilding(rectBuilding, "liangshi");
		//水
		rectBuilding.x = 600;
		rectBuilding.y = 0;
		initBuilding(rectBuilding, "shui");
		//衣服
		rectBuilding.x = 0;
		rectBuilding.y = 200;
		initBuilding(rectBuilding, "yifu");
		//书
		rectBuilding.x = 200;
		rectBuilding.y = 200;
		initBuilding(rectBuilding, "shu");
		//肉
		rectBuilding.x = 400;
		rectBuilding.y = 200;
		initBuilding(rectBuilding, "rou");
		//鱼
		rectBuilding.x = 600;
		rectBuilding.y = 200;
		initBuilding(rectBuilding, "yu");
	}
	{
		string sFileName = "./image/building_icon.png";
		bool bFlag = m_GuudSurface_building_icon.loadImage(sFileName.c_str());
		if (!bFlag) {
			g_GlobalData.m_GuudLog.log("[%s][%d]错误:图片 %s 加载失败", __FILE__,
					__LINE__, sFileName.c_str());
			exit(-1);
		}
		GuudRect rectBuilding;
		rectBuilding.w = 100;
		rectBuilding.h = 100;
		//木头
		rectBuilding.x = 0;
		rectBuilding.y = 0;
		initBuildingIcon(rectBuilding, "mutou");
		//石头
		rectBuilding.x = 100;
		rectBuilding.y = 0;
		initBuildingIcon(rectBuilding, "shitou");
		//粮食
		rectBuilding.x = 200;
		rectBuilding.y = 0;
		initBuildingIcon(rectBuilding, "liangshi");
		//水
		rectBuilding.x = 300;
		rectBuilding.y = 0;
		initBuildingIcon(rectBuilding, "shui");
		//衣服
		rectBuilding.x = 0;
		rectBuilding.y = 100;
		initBuildingIcon(rectBuilding, "yifu");
		//书
		rectBuilding.x = 100;
		rectBuilding.y = 100;
		initBuildingIcon(rectBuilding, "shu");
		//肉
		rectBuilding.x = 200;
		rectBuilding.y = 100;
		initBuildingIcon(rectBuilding, "rou");
		//鱼
		rectBuilding.x = 300;
		rectBuilding.y = 100;
		initBuildingIcon(rectBuilding, "yu");
	}
	m_bListen = true;
}
void CanvasControl::handleEvent(GuudEvent &stEvent) {
	if (m_bListen) {
		GuudPoint stPoint = m_gpScreenPosPixel;
		m_button_building.handleEvent(stPoint, stEvent, this);
		if (m_bListen_icon) {
			stPoint.m_iX += 200;
			int iNum = 0;
			map<string, GuudButton *>::iterator it;
			for (it = m_map_button_building_icon.begin();
					it != m_map_button_building_icon.end(); it++) {
				it->second->handleEvent(stPoint, stEvent, this);
				stPoint.m_iX += 100;
				iNum++;
				if (iNum % 4 == 0) {
					stPoint.m_iY += 100;
					stPoint.m_iX -= 400;
				}
			}
		}
	}
}
